//
//  BowlOfNooldModelLayer.m
//  demo_3
//
//  Created by 才 陈 on 12-3-5.
//  Copyright 2012年 sjtu. All rights reserved.
//

#import "BowlOfNooldModelLayer.h"

@implementation BowlOfNooldModelLayer

-(id) init
{
    if(self=[super initWithColor:ccc4(255, 255, 255, 255) width:BOWL_WIDTH height:BOWL_HEIHT])
    {
        [self setOpacity:0];
        
        [self addSoupBowl];
        
        [self setIsRelativeAnchorPoint:YES];
    }
    return self;
}

-(void) addSoupBowl
{
    NSString *soupBowlFileName = @"Bowl_soup_2x.png";
    soupBowl = [CCSprite spriteWithFile:soupBowlFileName];
    soupBowl.anchorPoint = CGPointMake(0, 0);
    [self addChild:soupBowl z:BOWL_SPRITE_Z_ORDER tag:BASIC_NOODLE_MATERIAL_TAG];
}

-(void) addNoodle
{
    if(noodle==Nil)
    {
        noodle = [CCSprite spriteWithFile:@"Bowl_noodle_2x.png"];
        //noodle.scale = scaling;
    }
    noodle.anchorPoint = CGPointMake(0, 0);
    [self addChild:noodle z:ADDITIONAL_FOOD_SPRITE_Z_ORDER tag:ADDITIONAL_NOODLE_MATERIAL_TAG];
}

-(void) addBeef
{
    if(beef==Nil)
    {
        beef = [CCSprite spriteWithFile:@"Bowl_beef_2x.png"];
        //beef.scale = scaling;
    }
    beef.anchorPoint = CGPointMake(0, 0);
    [self addChild:beef z:ADDITIONAL_FOOD_SPRITE_Z_ORDER tag:ADDITIONAL_NOODLE_MATERIAL_TAG];
}

-(void) addCucumber
{
    if(cucumber==Nil)
    {
        cucumber = [CCSprite spriteWithFile:@"Bowl_cucumber_2x.png"];
        //cucumber.scale = scaling;
    }
        //cucumber.anchorPoint = CGPointMake(0, 0);
    [self addChild:cucumber z:ADDITIONAL_FOOD_SPRITE_Z_ORDER tag:ADDITIONAL_NOODLE_MATERIAL_TAG];
}

-(void) addMutton
{
    if(mutton==Nil)
    {
        mutton = [CCSprite spriteWithFile:@"Bowl_mutton_2x.png"];
        //mutton.scale = scaling;
    }
    //mutton.anchorPoint = CGPointMake(0, 0);
    [self addChild:mutton z:ADDITIONAL_FOOD_SPRITE_Z_ORDER tag:ADDITIONAL_NOODLE_MATERIAL_TAG];
}

-(void) addRadish
{
    if(radish==Nil)
    {
        radish = [CCSprite spriteWithFile:@"Bowl_radish_2x.png"];
        //radish.scale = scaling;
    }
    //radish.anchorPoint = CGPointMake(0, 0);
    [self addChild:radish z:ADDITIONAL_FOOD_SPRITE_Z_ORDER tag:ADDITIONAL_NOODLE_MATERIAL_TAG];
}

-(void) prepareNewBowlOfNoodle
{
    [self removeAllChildrenWithCleanup:YES];
    [self addSoupBowl];
}

//the origin of bounding box if the left bottom point 
-(BOOL) isFallIntoBowl:(CCSprite *)sprite
{
    CGRect bowlBoundingBox = [self boundingBox];
    CGSize bowlSize = bowlBoundingBox.size;
    CGPoint bowlOrigin = bowlBoundingBox.origin;
    [NGSUtils printRect:bowlBoundingBox startWith:@"bowl_rect"];
    
    int hitBoxOriginX = bowlOrigin.x+bowlSize.width/5;
    int hitBoxOriginY = bowlOrigin.y+bowlSize.height*2/3;
    int hitBoxWidth = bowlSize.width*4/5;
    int hitBoxHeight = bowlSize.height/3;
    CGRect bowlHitBox = CGRectMake(hitBoxOriginX, hitBoxOriginY, hitBoxWidth, hitBoxHeight);
    [NGSUtils printRect:bowlHitBox startWith:@"boundingbox"];
    [NGSUtils printPoint:sprite.position startWith:@"sprite_position"];

   
    return CGRectContainsPoint(bowlHitBox, sprite.position);
}

+(id) beefNoodle:(float)scaling
{
    BowlOfNooldModelLayer *beefNoodle = [[BowlOfNooldModelLayer node] init];
    [beefNoodle addNoodle];
    [beefNoodle addBeef];
    [beefNoodle setScale:scaling];
    return beefNoodle;
}

+(id) muttonNoodle:(float)scaling
{
    BowlOfNooldModelLayer *muttonNoodle = [[BowlOfNooldModelLayer node] init];
    [muttonNoodle addNoodle];
    [muttonNoodle addBeef];
    [muttonNoodle setScale:scaling];
    
    return muttonNoodle;
}

+(id) cucumberNoodle:(float)scaling
{
    BowlOfNooldModelLayer *cucumberNoodle = [[BowlOfNooldModelLayer node] init];
    [cucumberNoodle addNoodle];
    [cucumberNoodle addBeef];
    [cucumberNoodle setScale:scaling];
    return cucumberNoodle;
}

+(id) newBowlOfNoodle:(float)scaling
{
    BowlOfNooldModelLayer *newBowlOfNoodle = [[BowlOfNooldModelLayer node] init];
    [newBowlOfNoodle setScale:scaling];
    return newBowlOfNoodle;
}

@end
